﻿using AutoMapper;
using FoodModel;
using J.FoodManageSet.Application.Command.TableNumCommand;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageSet.Application.CommandHandler.TableNumCommandHandler
{
    /// <summary>
    /// 桌号添加或修改
    /// </summary>
    public class TableNumAddCommandHandler : IRequestHandler<TableNumAddCommand, int>
    {
        private readonly ITableNumRepository _tableNumRepository;
        private readonly IMapper _mapper;

        public TableNumAddCommandHandler(ITableNumRepository tableNumRepository, IMapper mapper)
        {
            _tableNumRepository = tableNumRepository;
            _mapper = mapper;
        }
        public async Task<int> Handle(TableNumAddCommand request, CancellationToken cancellationToken)
        {
            int count = 0;
            if (request.TableNumId == 0)
            {
                
                TableNumModel model = _mapper.Map<TableNumModel>(request);
                count = await _tableNumRepository.NumAdd(model);
            }
            else
            {
                TableNumModel model = _mapper.Map<TableNumModel>(request);
                count = await _tableNumRepository.NumUpdate(model);
            }
            return count;
        }
    }
}
